<form  action="#{$smarty.const.ACT}#/SalOrderDetail/sal_order_detail_add/id/#{$one.id}#/ord_number/#{$one.ord_number}#/" method="post" class="pageForm required-validate" onsubmit="return validateCallback(this, navTabAjaxDone)">
<input type="hidden" name="order_number" value="#{$one.ord_number}#" />
  <div class="pageContent">
    <div class="pageFormContent" layoutH="58">
      <fieldset>
        <legend>订单信息：</legend>
        <p>
          <label>客户名称：</label>
          <input id="cusID" name="org.id" value="#{$one.cusID}#" type="hidden" />
          <input name="org.name" value="#{$customer[$one.cusID]}#" type="text"  readonly="readonly"/>
        </p>
        <p>
          <label>接收人：</label>
          <input name="linkman.id" value="#{$one.linkmanID}#" type="hidden"/>
          <input class="required" value="#{$linkman[$one.linkmanID]}#" name="linkman.name" type="text" postField="keyword" suggestFields="name" 
					suggestUrl="#{$smarty.const.ACT}#/CstLinkman/cst_linkman_select/cusID/{cusID}/" warn="请选择客户名称" lookupGroup="linkman" readonly="readonly"/>
        </p>
        <p>
          <label>订单主题：</label>
          <input name="title"  type="text" value="#{$one.title}#" readonly="readonly"/>
        </p>
        <p>
          <label>销售机会：</label>
          <input name="chance.id" value="#{$one.chanceID}#" type="hidden"/>
          <input name="chance.title" value="#{$chance[$one.chanceID]}#"  type="text" postField="keyword" suggestFields="title" 
					suggestUrl="#{$smarty.const.ACT}#/CstChance/cst_chance_select/cusID/{cusID}/" warn="请选择客户名称" lookupGroup="chance" readonly="readonly"/>
        </p>
        <p>
          <label>订单签订时间：</label>
          <input name="overdt" type="text" value="#{$one.bdt}#" readonly="readonly"/>
        </p>
        <p>
          <label>最晚发货时间：</label>
          <input name="lastsend" type="text" value="#{$one.edt}#" readonly="readonly"/>
        </p>
        <p>
          <label>我方代表：</label>
          <input name="our.id" value="#{$one.our_userID}#" type="hidden"/>
          <input name="our.name" value="#{$users[$one.our_userID]}#" type="text" postField="keyword" suggestFields="title" 
					suggestUrl="#{$smarty.const.ACT}#/User/cst_chance_select/cusID/{cusID}/" warn="请选择客户名称" lookupGroup="our" readonly="readonly"/>
        </p>
        <p>
          <label>建档时间：</label>
          <input name="adt" type="text" value="#{$one.adt}#" readonly="readonly"/>
        </p>
      </fieldset>
      <div class="divider"></div>
      <div class="tabs">
        <div class="tabsHeader">
          <div class="tabsHeaderContent">
            <ul>
              <li class="selected"><a href="javascript:void(0)"><span>订单明细</span></a></li>
            </ul>
          </div>
        </div>
        <div class="tabsContent">
          <div>
            <table class="list nowrap itemDetail" addButton="添加产品行数" width="100%">
              <thead>
                <tr>
                  <th type="lookup" name="items.name[]" lookupGroup="items" lookupUrl="#{$smarty.const.ACT}#/Product/lookup_search/" suggestFields="name[],pro_number[],model[],norm[],price[]" fieldClass="required">产品名称</th>
                  <th type="text" name="items.pro_number[]" size="12" fieldClass="required">产品编号</th>
                  <th type="text" name="items.model[]"  size="12" >型号</th>
                  <th type="text" name="items.norm[]"  size="12" >规格</th>
                  <th type="calculate" name="items.price[]" size="5" fieldClass="digits required">单 价(￥)</th>
                  <th type="calculate" name="items.orgCount[]" fieldClass="required digits" size="4">數 量</th>
                  <th type="calculate" name="items.orgDiscount[]" size="2">折 扣(%)</th>
                  <th type="text" name="items.orgTotal[]" size="8" readOnly>金 额(￥)</th>
                  <th onclick="calculate(this,1)" type="del" fieldClass="refreshAmount">删行</th>
                </tr>
              </thead>
              <tbody>
              #{foreach from=$list key=k item=v}#
              <tr class="unitBox">
                <td><input  name="items.name[]" type="text" postField="keyword" suggestFields="items.name[],pro_number[],model[],norm[],price[]" fieldClass="required" suggestUrl="#{$smarty.const.ACT}#/Product/lookup_search/" lookupGroup="items" class="required" value="#{$v.name}#" size="12"/><a class="btnLook" href="#{$smarty.const.ACT}#/Product/lookup_search/" lookupGroup="items" suggestFields="items.name[],pro_number[],model[],norm[],price[]" suffix="[]" >查找带回</a></td>
                <td><input type="text" name="items.pro_number[]" value="#{$v.pro_number}#" size="12" class="textInput"></td>
                <td><input type="text" name="items.model[]"  size="12" value="#{$v.model}#"></td>
                <td><input type="text" name="items.norm[]"  size="12" value="#{$v.norm}#"></td>
                <td><input type="text" name="items.price[]" size="5" value="#{$v.price}#" onkeyup="calculate(this, 0);"  Class="digits required"></td>
                <td><input type="text" name="items.orgCount[]" value="#{$v.number}#" onkeyup="calculate(this, 0);"  size="4" Class="digits required"></td>
                <td><input type="text" name="items.orgDiscount[]"  value="#{$v.rebate}#" onkeyup="calculate(this, 0);"  size="2"></td>
                <td><input type="text" name="items.orgTotal[]" value="#{$v.money}#" size="8" readOnly>
                  </th>
                <td><a href="javascript:void(0)" onclick="calculate(this, 1);" class="btnDel"></a>
                  </td>
              </tr>
              #{/foreach}#
              </tbody>
              
            </table>
          </div>
        </div>
        <div class="tabsFooter">
          <div class="tabsFooterContent"></div>
        </div>
      </div>
    </div>
    <div class="formBar">
      <label style="float:left">累计数量:
        <input name="total_nums" size="3" type="text" readonly="readonly" />
      </label>
      <label style="float:left"> 折扣金额:
        <input name="odd_amount" size="5" type="text" />
      </label>
      <label style="float:left"> 应付金额:
        <input name="total_amount" size="8" type="text" readonly="readonly" />
      </label>
      <label style="float:left"> 实付金额:
        <input name="trade_amount" size="8" type="text" />
      </label>
<!--          <label style="float:left">  制单人:<input name="createMan" value="{$Think.session.loginUserName}" size="10" type="text" readonly="true" /></label>    
	<label style="float: left;">  入库:<input type="checkbox" id="goStock" name="goStock" value="1" checked="checked" /></label>
    <label style="float: left;">   付款:<input type="checkbox" id="goFunds" name="goFunds" value="1" checked="checked" /></label>
    <label style="float:left">   付款账户:
    <SELECT name="bankid">
     <volist id="vu" name="listBank">
	    <option value="{$vu.id}">{$vu.bankname}</option>
     </volist>
	</SELECT>-->
      </label>
      <ul>
        <li>
          <div class="buttonActive">
            <div class="buttonContent">
              <button type="submit">保存</button>
            </div>
          </div>
        </li>
        <li>
          <div class="button">
            <div class="buttonContent">
              <button class="close" type="button">关闭</button>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</form>
<script type="text/javascript">
<!--
var total_amount = $("input[name=total_amount]");
var trade_amount = $("input[name=trade_amount]");
var odd_amount = $("input[name=odd_amount]");
var total_nums = $("input[name=total_nums]");
$(function(){
    
    // 初始化應收金額和實收金額
	total_amount.val((0).toFixed(2));
	trade_amount.val((0).toFixed(2));
   total_nums.val(0);
	
	// 扣除零頭
	odd_amount.keyup(function(){
		trade_amount.val((total_amount.val() - $(this).val()).toFixed(2));
	});
});

function calculate(t, del){
    var tbody = $(t).parents('tbody');
    var total = 0;
    var totalnums = 0;
    var fix = 0; 
    var fixnums=0;// 刪除的話應該忽略本行
    if (del == 1){
        $(t).parents("tr:first").each(function(){
            var orgCount = 0;
            var price = 0;
            var orgDiscount = 1;
            var orgTotal = 0;
            $(this).find('input').each(function(){
                if (this.name == 'items.orgCount[]') orgCount = this.value == '' ? 0 : this.value;
                if (this.name == 'items.price[]') price = this.value == '' ? 0 : this.value;
                if (this.name == 'items.orgDiscount[]') orgDiscount = this.value == '' ? 1 : (1 - this.value/100);
                if (this.name == 'items.orgTotal[]'){
                    orgTotal = orgCount*price*orgDiscount;
                    if (orgTotal == 0){
                        this.value = '';
                    } else {
                        this.value = orgTotal.toFixed(2);
                    }
                }
            });
            fix = orgTotal;
            fixnums =orgCount;
        });
    }
    $(tbody).find('tr').each(function(){
        var orgCount = 0;
        var price = 0;
        var orgDiscount = 1;
        var orgTotal = 0;
        $(this).find('input').each(function(){
            if (this.name == 'items.orgCount[]') orgCount = this.value == '' ? 0 : 1*this.value;
            if (this.name == 'items.price[]') price = this.value == '' ? 0 : this.value;
            if (this.name == 'items.orgDiscount[]') orgDiscount = this.value == '' ? 1 : (1 - this.value/100);
            if (this.name == 'items.orgTotal[]'){
                orgTotal = orgCount*price*orgDiscount;
				hitTotal = orgCount*price;
                if (orgTotal == 0){
                    this.value = '';
                } else {
                    this.value = orgTotal.toFixed(2);
                }
            }
        });
        total += orgTotal;
        totalnums +=orgCount;
    });
    if (fix != 0){
        total -= fix;
        totalnums -= fixnums;
    }
    total_nums.val(totalnums);
    total_amount.val(total.toFixed(2));
    trade_amount.val((total - odd_amount.val()).toFixed(2));


}
//-->
</script>